﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Common;

public partial class website_admin_insurance_productcategoryeditor : PageBase
{
    public string pageTitle = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        if (RequestBase.GetFormString("__EVENTTARGET") == "saveinfo")
        {
            SaveInfo(RequestBase.GetFormString("__EVENTARGUMENT"));
        }

        if (!Page.IsPostBack)
        {
            ProductDataBind();

            if (Utils.IsGuid(RequestBase.GetQueryString("productid")))
            {
                pageTitle = "修改";
                Initialize(RequestBase.GetQueryString("productid"));
            }
            else
            {
                pageTitle = "添加";
                dropCategory.SelectedValue = RequestBase.GetQueryString("parentid");
                if (RequestBase.GetQueryString("parentid") != "00000000-0000-0000-0000-000000000000" && RequestBase.GetQueryString("parentid") != "")
                {
                    Model.Insurance.InsuranceProductBase _obj = BLL.Insurance.InsuranceProductBase.GetProductTreeObjectById(RequestBase.GetQueryString("parentid")); 
                    txtInnerCode.Value = _obj.InternalCode;
                }
            }
        }
    }

    #region 类别数据初始化
    void Initialize(string productid)
    {
        Model.Insurance.InsuranceProductBase _obj = BLL.Insurance.InsuranceProductBase.GetProductTreeObjectById(productid);
        ProductObj = _obj;

        if (_obj.IsProtected)
        {
            dropCategory.Enabled = false;
            cbxIsProtected.Disabled = true;
        }
    }
    #endregion

    #region 类别列表数据绑定
    void ProductDataBind()
    {
        StringBuilder SQL = new StringBuilder().Append(" AND DeletionStateCode = 0");
        if (Utils.IsNumber(RequestBase.GetQueryString("typecode")))
            SQL.Append(" AND TypeCode = " + RequestBase.GetQueryString("typecode"));

        DataTable dt = BLL.CommonBLL.Select("InsuranceProductBase", "*", SQL.ToString() + " ORDER BY LineNum ASC");
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["ProductName"].ToString(), d["ProductId"].ToString());
            dropCategory.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["ProductId"].ToString().Trim(), dt);
        }
    }
    #endregion

    public Model.Insurance.InsuranceProductBase ProductObj
    {
        get
        {
            Model.Insurance.InsuranceProductBase _product = new Model.Insurance.InsuranceProductBase();
            if (Utils.IsGuid(RequestBase.GetQueryString("productid")))
                _product.ProductId = RequestBase.GetQueryString("productid");

            _product.Description = txtDescription.Value.Trim();
            _product.InternalCode = txtInnerCode.Value;
            _product.LineNum = txtLineNum.Value.Trim();
            _product.ParentId = dropCategory.SelectedValue;
            _product.ProductName = txtProductName.Value.Trim();
            _product.StatusCode = int.Parse(selStatus.Value);
            _product.ProductNumber = txtNumber.Value;
            _product.TypeCode = int.Parse(txtTypeCode.Value.Trim());
            _product.ProtectItems = txtProtectItems.Value.Trim();
            _product.IsProtected = cbxIsProtected.Checked;
             
            for (var i = 0; i < 3; i++)
            {
                if (((HtmlInputCheckBox)this.FindControl("cbxAreaCodeList_" + i.ToString())).Checked)
                    _product.AreaCodeList += i.ToString() + ","; 
            }
            if (_product.AreaCodeList != "")
                _product.AreaCodeList = _product.AreaCodeList.Substring(0, _product.AreaCodeList.Length - 1);

            return _product;
        }
        set
        {
            if (value != null)
            {
                txtDescription.Value = value.Description;
                txtInnerCode.Value = value.InternalCode;
                txtLineNum.Value = value.LineNum;
                txtCategoryValue.Value = value.ParentId;
                txtOriginalParentId.Value = value.ParentId;
                txtProductName.Value = value.ProductName;
                selStatus.Value = value.StatusCode.ToString();
                dropCategory.SelectedValue = value.ParentId;
                txtNumber.Value = value.ProductNumber;
                cbxIsProtected.Checked = value.IsProtected;
                 
                if (value.IsProtected)
                    dropCategory.Enabled = false;
                 
                txtTypeCode.Value = value.TypeCode.ToString();
                txtProtectItems.Value = value.ProtectItems.Trim();

                for (var i = 0; i < value.AreaCodeList.Split(',').Length; i++)
                {
                    if (((HtmlInputCheckBox)this.FindControl("cbxAreaCodeList_" + value.AreaCodeList.Split(',')[i].ToString())) != null)
                        ((HtmlInputCheckBox)this.FindControl("cbxAreaCodeList_" + value.AreaCodeList.Split(',')[i].ToString())).Checked = true;
                }
            }
        }
    }

    #region 信息处理
    /// <summary>
    /// 信息处理
    /// </summary>
    void SaveInfo(string omode)
    { 
        string result = "";

        if (RequestBase.GetQueryString("otype") == "add")
        {
            result = BLL.Insurance.InsuranceProductBase.ProductBase_Tree_Create(ProductObj); 
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            result = BLL.Insurance.InsuranceProductBase.Modify(ProductObj, (dropCategory.SelectedValue.Trim() == txtOriginalParentId.Value.Trim()) ? false : true);
        }
 
        if (result.IndexOf("succeeded") >= 0)
        {
            if (omode == "saveandclose")
                JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.document.location.reload(); window.close();");
            else
                if (Utils.IsNumber(RequestBase.GetQueryString("typecode")))
                    JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.document.location.reload(); window.location.href='productcategoryeditor.aspx?otype=add&parentid=" + dropCategory.SelectedValue.Trim() + "&typecode=" + RequestBase.GetQueryString("typecode") + "';");
                else
                    JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.document.location.reload(); window.location.href='productcategoryeditor.aspx?otype=add&parentid=" + dropCategory.SelectedValue.Trim() + "';");
        }
        else
        {
            this.Page.RegisterStartupScript("writejs", "<script>alert('" + result + "');</script>");
            return;
        }
    }
    #endregion
}
